package com.saifan.wyy_ov.ui.user;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.model.impl.GetData;
import com.saifan.wyy_ov.model.interfaces.ResponseData;
import com.saifan.wyy_ov.ui.view.BaseActivity;
import com.saifan.wyy_ov.utils.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import lomasky.ma.utils.SharedPrefUtil;

/**
 * Created by ma on 2015/7/21.
 */
public class ForgotActivity extends BaseActivity {
    private android.support.v7.widget.Toolbar toolbar;
    private android.widget.EditText userName;
    private android.widget.EditText numberId;
    public void startActivity(Class clazz) {
        startActivity(new Intent(this, clazz));
    }
    public boolean isEmpty(TextView view) {
        return view.getText().toString().trim().isEmpty();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView();
    }

    void setContentView() {
        setContentView(R.layout.forgot_activity);
        this.numberId = (EditText) findViewById(R.id.numberId);
        this.userName = (EditText) findViewById(R.id.userName);
        this.toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    }

    public void sure(View v) {
        if (isEmpty(userName)){
            ToastUtil.showToast(this,"用户名不能为空！");
            return;
        }
        if (isEmpty(numberId)){
            ToastUtil.showToast(this,"证件号码不能为空！");
            return;
        }


        try {

            GetData getData = new GetData();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("WYKHDA_KHYHM", userName.getText().toString().trim());
            jsonObject.put("WYKHDA_ZJHM", numberId.getText().toString().trim());
            getData.getDataJson(this, Api.Customer.FORGET_PASSWORD, jsonObject, getString(R.string.wait), new ResponseData() {
                @Override
                public void onSuccess(String json) {

                    ToastUtil.showToast(ForgotActivity.this,"密码已重置为123456");
                    SharedPrefUtil.remove(ForgotActivity.this, "Password");
                    startActivity(LoginActivity.class);
                    finish();
                }

                @Override
                public void onFailed(String str) {
                    ToastUtil.showToast(ForgotActivity.this,"密码未能重置，请检查用户名和证件号码");
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }


    }

}
